#include<QThread>
#include<QDebug>
#include <QDateTime>

#include "outwork.h"
#define MYLOG qDebug() << "[" << __FILE__ << ":" << __LINE__ << ":" << QThread::currentThread() << __func__ << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz")<< "]"
//构造函数初始化
OutWork::OutWork(QObject *parent)
{
//    vecState(TargetNumber,1);

}

OutWork::~OutWork()
{

}

void OutWork::toclearTarget(int addr)
{
    if(addr == -1)
    {
        oneTargetBullet.clear();
    }else{
        oneTargetBullet.remove(addr);
    }
    emit currentTargetBullet(oneTargetBullet);
}

//QJsonDocument、QJsonObject、QVariantMap可以互相转化

//根据自定义任务重载run()函数
void OutWork::doWork(QByteArray temp)
{
      try{

//            QByteArray recvAllMsg = d_queue.dequeue();

            QString recvStr = QString::fromUtf8(temp);

            //构建JSON对象
            QJsonDocument jsonDocument = QJsonDocument::fromJson(recvStr.toLocal8Bit().data());

            if(jsonDocument.isNull())
              {
                MYLOG<<"jsonDocument is null";

              }

            QJsonObject json = jsonDocument.object();

            //  Json类型转QMap类型
            QVariantMap map = json.toVariantMap();

            code = map["code"].toInt();
            QMap<QString, QVariant> dataMap = map["data"].toMap();
            QMap<QString, QVariant> msgMap = map["msg"].toMap();
            //  确定靶标连接状态
            if(code == GetTargetState){
//                for(int i = 0;i < TargetNumber;i++)
//                  {
//                    vecState[i] = 1;
//                  }
                QVector<int>vecState(TargetNumber,1);

//                QMap<QString, QVariant> map1 = map["data"].toMap();
                MYLOG<<dataMap;
                QMap<QString, QVariant>::iterator it = dataMap.begin();
                while(it != dataMap.end())
                  {
                    int key = it.key().toInt();
                    if(key >= 0 && key < 21)
                      {
                        vecState[key] = it.value().toInt();
                        it++;
                      }
                  }

                emit mySignalState(vecState);
              }
            else if(code == GetVoltage){//获取电量信息

//                QMap<QString, QVariant> map1 = map["data"].toMap();

                QMap<QString, QVariant>::iterator it = dataMap.begin();

                QVector<double> vecBattery(TargetNumber,0);
                while(it != dataMap.end())
                  {
                    int key = it.key().toInt();
                    if(key >= 0 && key < 21)
                      {
                        vecBattery[key] = it.value().toDouble();

                      }
                    it++;
                  }
                //获取所有靶的电量信息
                emit mySignalBattery(vecBattery);

              }else if(code == CurrentBullet){
                OneTaregtBullet targetBullet;
                targetBullet.addr=dataMap["addr"].toInt();
                targetBullet.x=dataMap["x"].toDouble();
                targetBullet.y=dataMap["y"].toDouble();
                targetBullet.cylinder_number=dataMap["cn"].toInt();
                targetBullet.direction=dataMap["dir"].toInt();
                oneTargetBullet[targetBullet.addr].append(targetBullet);
                emit currentTargetBullet(oneTargetBullet);

              }else if(code == scoreBullet){
//                int len = searchDate.size();
//                QVector<QVariant> data;
//                for(int i=0;i<len;i++)
//                {
//                    QVariant variant = QVariant::fromValue(searchDate[i]);
//                    data.append(variant);
//                }

                emit scoreTargetInfo(searchDate);

              }else if(code == BindInfo){
//                QMap<QString, QVariant> map2 = map["msg"].toMap();
                QString msg = msgMap["msg"].toString();
                emit mySignalInfo(msg);
              }else if(code == ClearTarget){
                int addr = dataMap["addr"].toInt();
                toclearTarget(addr);
            }

        //挂起1ms
        QThread::msleep(100);
      }
      catch(std::exception &ex)
      {
       qWarning()<<"OutWork:doWork"<<ex.what();

      }
}

//void OutWork::doWork()
//{
//      try{
//       if(!d_queue.isEmpty()){

//                QByteArray recvAllMsg = d_queue.dequeue();

//              QString recvStr = QString::fromUtf8(recvAllMsg);

//              //构建JSON对象
//              QJsonDocument jsonDocument = QJsonDocument::fromJson(recvStr.toLocal8Bit().data());

//              if(jsonDocument.isNull())
//              {
//                MYLOG<<"jsonDocument is null";

//              }

//              QJsonObject json = jsonDocument.object();

//              //  Json转QMap
//              QVariantMap map = json.toVariantMap();

//              code = map["code"].toInt();

//              qDebug()<<"code:  "<<code;

//              //  确定靶标连接状态
//              if(code == GetTargetState){
//                //                for(int i = 0;i < TargetNumber;i++)
//                //                  {
//                //                    vecState[i] = 1;
//                //                  }
//                QVector<int>vecState(TargetNumber,1);

//                QMap<QString, QVariant> map1 = map["data"].toMap();
//                MYLOG<<map1;
//                QMap<QString, QVariant>::iterator it = map1.begin();
//                while(it != map1.end())
//                {
//                    int key = it.key().toInt();
//                    if(key >= 0 && key < 21)
//                    {
//                        vecState[key] = it.value().toInt();
//                        it++;
//                    }
//                }

//                emit mySignalState(vecState);
//              }
//              else if(code == GetVoltage){//获取电量信息

//                QMap<QString, QVariant> map1 = map["data"].toMap();

//                QMap<QString, QVariant>::iterator it = map1.begin();

//                QVector<double> vecBattery(TargetNumber,0);
//                while(it != map1.end())
//                {
//                    int key = it.key().toInt();
//                    if(key >= 0 && key < 21)
//                    {
//                        vecBattery[key] = it.value().toDouble();

//                    }
//                    it++;
//                }
//                //获取所有靶的电量信息
//                emit mySignalBattery(vecBattery);

//              }else if(code == BindInfo)
//              {
//                QMap<QString, QVariant> map2 = map["msg"].toMap();
//                QString msg = map2["msg"].toString();
//                emit mySignalInfo(msg);
//              }
//       }
//       //挂起1ms
//       QThread::msleep(100);
//      }
//      catch(std::exception &ex)
//      {
//       qWarning()<<"OutWork:doWork"<<ex.what();

//      }
//}

